package com.sun.dao;

import com.sun.dto.SearchBlog;
import com.sun.dto.TagBlog;
import com.sun.pojo.Blog;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: glorysun
 * @date: 2020/8/3
 */
@Mapper
@Repository
public interface BlogDao {

    @Select("select * from blog.t_blog where id=#{id}")
    @ResultMap("BlogMap")
    Blog selectBlogById(Long id);   //根据id查询博客

    @Select("<script>" +
            "<bind name=\"pattern\" value=\"'%' + title + '%'\" />" +
            "select * from blog.t_blog where title like #{pattern}" +
            "</script>")
    @ResultMap("BlogMap")
    List<Blog> selectBlogByTitle(String title);   //根据标题查询

    //@SelectProvider(type = com.sun.dao.BlogSqlProvider.class,method = "selectBlogByTitleOrTypeOrRecommend")
    @Select("<script>" +
            "SELECT * FROM blog.t_blog WHERE 1=1" +
            "<bind name=\"pattern\" value=\"'%' + title + '%'\" />" +
            "<if test=\"title!=null and title!=''\">and title like #{pattern}</if>" +
            "<if test=\"typeId!=null and typeId!=''\">and type_id=#{typeId}</if>" +
            "<if test=\"recommend==true\">and recommend=true</if> order by update_time desc" +
            "</script>")
    @ResultMap("BlogMap")
    List<Blog> selectBlogByCondition(SearchBlog searchBlog); //条件查询

    @Select("select * from blog.t_blog where type_id=#{id}  order by update_time desc")
    @ResultMap("BlogMap")
    List<Blog> selectBlogByType(Long id);   //根据分类查询博客

    @Select("SELECT b.* \n" +
            "FROM t_blog_tags bt left join t_blog b\n" +
            "ON b.id=bt.blog_id\n" +
            "where bt.tag_id=#{id}")
    @ResultMap("BlogMap")
    List<Blog> selectBlogByTag(Long id);    //根据标签查询博客

    @Select("select * from blog.t_blog order by update_time desc")
    @Results(id = "BlogMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "type_id",property = "typeId"),
            @Result(column = "user_id",property = "userId"),
            @Result(property = "type",column = "type_id",
                    one = @One(select = "com.sun.dao.TypeDao.selectTypeById",fetchType = FetchType.LAZY)),
            @Result(property = "user",column = "user_id",
                    one = @One(select = "com.sun.dao.UserDao.selectById",fetchType = FetchType.LAZY)),
            @Result(property = "tags",column = "id",
                    many = @Many(select = "com.sun.dao.TagDao.selectTagByBlogId",fetchType = FetchType.LAZY)),
            @Result(property = "comments",column = "id",
                    many = @Many(select = "com.sun.dao.CommentDao.selectCommentByBlogIdTop",fetchType = FetchType.LAZY)),
    })
    List<Blog> selectBlogList();    //查询所有博客

    @Select("select b.* from blog.t_comment c LEFT JOIN blog.t_blog b ON c.blog_id=b.id \n" +
            "GROUP BY c.blog_id ")
    List<Blog> selectBlogListByComment();   //查询有留言的博客

    @Select("select * from blog.t_blog order by update_time desc limit 10")
    List<Blog> selectBlogListTen();    //按更新时间查询10个博客

    @Insert("insert into blog.t_blog (id,title, content, first_picture, flag,\n" +
            "        views, appreciation, share_statement, commentabled,published,\n" +
            "        recommend, create_time, update_time, type_id,tag_ids, user_id, description)\n" +
            "        values (#{id},#{title},#{content},#{firstPicture},#{flag},#{views},#{appreciation},\n" +
            "        #{shareStatement},#{commentabled},#{published},#{recommend},#{createTime},\n" +
            "        #{updateTime},#{typeId},#{tagIds},#{userId},#{description});")
    int insertBlog(Blog blog);  //新增博客

    @Insert("insert into blog.t_blog_tags (tag_id,blog_id) values (#{tagId},#{blogId})")
    int insertTagBlog(TagBlog tagBlog);  //新增标签博客信息

    @Update("update blog.t_blog set published = #{published},flag = #{flag} ,\n" +
            "        title = #{title}, content = #{content}, type_id = #{typeId}, tag_ids = #{tagIds},\n" +
            "        first_picture = #{firstPicture} , description = #{description} , recommend = #{recommend} ,\n" +
            "        share_statement = #{shareStatement}, appreciation = #{appreciation},\n" +
            "        commentabled = #{commentabled} ,update_time = #{updateTime} where id = #{id};")
    int updateBlog(Blog blog);  //更新博客

    @Delete("delete from blog.t_blog where id=#{id}")
    int deleteBlog(Long id);    //删除博客


    @Delete("delete from blog.t_blog_tags where blog_id=#{id}")
    int deleteTagBlog(Long id); //删除标签博客信息;
}
